<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
  <head>
    <meta charset="utf-8" />
    <meta name="generator" content="pandoc" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, user-scalable=yes"
    />
    <title>appendix</title>
    <style type="text/css">
      code {
        white-space: pre-wrap;
      }
      span.smallcaps {
        font-variant: small-caps;
      }
      span.underline {
        text-decoration: underline;
      }
      div.column {
        display: inline-block;
        vertical-align: top;
        width: 50%;
      }
    </style>
  </head>
  <body>
    <h3 id="navigation">Navigation</h3>
    <ul>
      <li>
        <a href="https://docs.python.org/3/genindex.html" title="General Index"
          >index</a
        >
      </li>
      <li>
        <a
          href="https://docs.python.org/3/py-modindex.html"
          title="Python Module Index"
          >modules</a
        >
        |
      </li>
      <li>
        <a
          href="https://docs.python.org/3/using/index.html"
          title="Python Setup and Usage"
          >next</a
        >
        |
      </li>
      <li>
        <a
          href="floatingpoint.html"
          title="15. Floating Point Arithmetic: Issues and Limitations"
          >previous</a
        >
        |
      </li>
      <li><img src="../_static/py.png" /></li>
      <li><a href="https://www.python.org/">Python</a> »</li>
      <li>
        <a href="https://docs.python.org/3/index.html">3.9.5 Documentation</a> »
      </li>
      <li><a href="index.html">The Python Tutorial</a> »</li>
      <li><div class="line-block"></div></li>
    </ul>
    <p><span id="tut-appendix"></span></p>
    <h1 id="appendix">
      <span class="section-number">16. </span>Appendix<a
        href="#appendix"
        class="headerlink"
        title="Permalink to this headline"
        >¶</a
      >
    </h1>
    <p><span id="tut-interac"></span></p>
    <h2 id="interactive-mode">
      <span class="section-number">16.1. </span>Interactive Mode<a
        href="#interactive-mode"
        class="headerlink"
        title="Permalink to this headline"
        >¶</a
      >
    </h2>
    <p><span id="tut-error"></span></p>
    <h3 id="error-handling">
      <span class="section-number">16.1.1. </span>Error Handling<a
        href="#error-handling"
        class="headerlink"
        title="Permalink to this headline"
        >¶</a
      >
    </h3>
    <p>
      When an error occurs, the interpreter prints an error message and a stack
      trace. In interactive mode, it then returns to the primary prompt; when
      input came from a file, it exits with a nonzero exit status after printing
      the stack trace. (Exceptions handled by an
      <a
        href="https://docs.python.org/3/reference/compound_stmts.html#except"
        class="reference internal"
        ><code class="xref std std-keyword docutils literal notranslate"
          >except</code
        ></a
      >
      clause in a
      <a
        href="https://docs.python.org/3/reference/compound_stmts.html#try"
        class="reference internal"
        ><code class="xref std std-keyword docutils literal notranslate"
          >try</code
        ></a
      >
      statement are not errors in this context.) Some errors are unconditionally
      fatal and cause an exit with a nonzero exit; this applies to internal
      inconsistencies and some cases of running out of memory. All error
      messages are written to the standard error stream; normal output from
      executed commands is written to standard output.
    </p>
    <p>
      Typing the interrupt character (usually Control-C or Delete) to the
      primary or secondary prompt cancels the input and returns to the primary
      prompt.
      <a href="#id2" id="id1" class="footnote-reference brackets">1</a> Typing
      an interrupt while a command is executing raises the
      <a
        href="https://docs.python.org/3/library/exceptions.html#KeyboardInterrupt"
        class="reference internal"
        title="KeyboardInterrupt"
        ><code class="sourceCode python"
          ><span class="pp">KeyboardInterrupt</span></code
        ></a
      >
      exception, which may be handled by a
      <a
        href="https://docs.python.org/3/reference/compound_stmts.html#try"
        class="reference internal"
        ><code class="xref std std-keyword docutils literal notranslate"
          >try</code
        ></a
      >
      statement.
    </p>
    <p><span id="tut-scripts"></span></p>
    <h3 id="executable-python-scripts">
      <span class="section-number">16.1.2. </span>Executable Python Scripts<a
        href="#executable-python-scripts"
        class="headerlink"
        title="Permalink to this headline"
        >¶</a
      >
    </h3>
    <p>
      On BSD’ish Unix systems, Python scripts can be made directly executable,
      like shell scripts, by putting the line
    </p>
    <pre><code>#!/usr/bin/env python3.5</code></pre>
    <p>
      (assuming that the interpreter is on the user’s
      <span id="index-0" class="target"></span><code>PATH</code>) at the
      beginning of the script and giving the file an executable mode. The
      <code>#!</code> must be the first two characters of the file. On some
      platforms, this first line must end with a Unix-style line ending
      (<code>'\n'</code>), not a Windows (<code>'\r\n'</code>) line ending. Note
      that the hash, or pound, character, <code>'#'</code>, is used to start a
      comment in Python.
    </p>
    <p>
      The script can be given an executable mode, or permission, using the
      <strong>chmod</strong> command.
    </p>
    <pre><code>$ chmod +x myscript.py</code></pre>
    <p>
      On Windows systems, there is no notion of an “executable mode”. The Python
      installer automatically associates <code>.py</code> files with
      <code>python.exe</code> so that a double-click on a Python file will run
      it as a script. The extension can also be <code>.pyw</code>, in that case,
      the console window that normally appears is suppressed.
    </p>
    <p><span id="tut-startup"></span></p>
    <h3 id="the-interactive-startup-file">
      <span class="section-number">16.1.3. </span>The Interactive Startup File<a
        href="#the-interactive-startup-file"
        class="headerlink"
        title="Permalink to this headline"
        >¶</a
      >
    </h3>
    <p>
      When you use Python interactively, it is frequently handy to have some
      standard commands executed every time the interpreter is started. You can
      do this by setting an environment variable named
      <span id="index-1" class="target"></span
      ><a
        href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP"
        class="reference internal"
        ><code class="xref std std-envvar docutils literal notranslate"
          >PYTHONSTARTUP</code
        ></a
      >
      to the name of a file containing your start-up commands. This is similar
      to the <code>.profile</code> feature of the Unix shells.
    </p>
    <p>
      This file is only read in interactive sessions, not when Python reads
      commands from a script, and not when <code>/dev/tty</code> is given as the
      explicit source of commands (which otherwise behaves like an interactive
      session). It is executed in the same namespace where interactive commands
      are executed, so that objects that it defines or imports can be used
      without qualification in the interactive session. You can also change the
      prompts <code>sys.ps1</code> and <code>sys.ps2</code> in this file.
    </p>
    <p>
      If you want to read an additional start-up file from the current
      directory, you can program this in the global start-up file using code
      like
      <code
        >if os.path.isfile('.pythonrc.py'):
        exec(open('.pythonrc.py').read())</code
      >. If you want to use the startup file in a script, you must do this
      explicitly in the script:
    </p>
    <pre><code>import os
filename = os.environ.get(&#39;PYTHONSTARTUP&#39;)
if filename and os.path.isfile(filename):
    with open(filename) as fobj:
        startup_file = fobj.read()
    exec(startup_file)</code></pre>
    <p><span id="tut-customize"></span></p>
    <h3 id="the-customization-modules">
      <span class="section-number">16.1.4. </span>The Customization Modules<a
        href="#the-customization-modules"
        class="headerlink"
        title="Permalink to this headline"
        >¶</a
      >
    </h3>
    <p>
      Python provides two hooks to let you customize it:
      <code>sitecustomize</code> and <code>usercustomize</code>. To see how it
      works, you need first to find the location of your user site-packages
      directory. Start Python and run this code:
    </p>
    <pre><code>&gt;&gt;&gt; import site
&gt;&gt;&gt; site.getusersitepackages()
&#39;/home/user/.local/lib/python3.5/site-packages&#39;</code></pre>
    <p>
      Now you can create a file named <code>usercustomize.py</code> in that
      directory and put anything you want in it. It will affect every invocation
      of Python, unless it is started with the
      <a
        href="https://docs.python.org/3/using/cmdline.html#cmdoption-s"
        class="reference internal"
        ><code class="xref std std-option docutils literal notranslate"
          >-s</code
        ></a
      >
      option to disable the automatic import.
    </p>
    <p>
      <code>sitecustomize</code> works in the same way, but is typically created
      by an administrator of the computer in the global site-packages directory,
      and is imported before <code>usercustomize</code>. See the documentation
      of the
      <a
        href="https://docs.python.org/3/library/site.html#module-site"
        class="reference internal"
        title="site: Module responsible for site-specific configuration."
        ><code class="sourceCode python">site</code></a
      >
      module for more details.
    </p>
    <p>Footnotes</p>
    <p>
      <span class="brackets"><a href="#id1" class="fn-backref">1</a></span
      ><br />
      A problem with the GNU Readline package may prevent this.
    </p>
    <h3 id="table-of-contents">
      <a href="https://docs.python.org/3/contents.html">Table of Contents</a>
    </h3>
    <ul>
      <li>
        <a href="#" class="reference internal">16. Appendix</a>
        <ul>
          <li>
            <a href="#interactive-mode" class="reference internal"
              >16.1. Interactive Mode</a
            >
            <ul>
              <li>
                <a href="#error-handling" class="reference internal"
                  >16.1.1. Error Handling</a
                >
              </li>
              <li>
                <a href="#executable-python-scripts" class="reference internal"
                  >16.1.2. Executable Python Scripts</a
                >
              </li>
              <li>
                <a
                  href="#the-interactive-startup-file"
                  class="reference internal"
                  >16.1.3. The Interactive Startup File</a
                >
              </li>
              <li>
                <a href="#the-customization-modules" class="reference internal"
                  >16.1.4. The Customization Modules</a
                >
              </li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
    <h4 id="previous-topic">Previous topic</h4>
    <p>
      <a href="floatingpoint.html" title="previous chapter"
        ><span class="section-number">15. </span>Floating Point Arithmetic:
        Issues and Limitations</a
      >
    </p>
    <h4 id="next-topic">Next topic</h4>
    <p>
      <a href="https://docs.python.org/3/using/index.html" title="next chapter"
        >Python Setup and Usage</a
      >
    </p>
    <h3 id="this-page">This Page</h3>
    <ul>
      <li><a href="https://docs.python.org/3/bugs.html">Report a Bug</a></li>
      <li>
        <a
          href="https://github.com/python/cpython/blob/3.9/Doc/tutorial/appendix.rst"
          >Show Source</a
        >
      </li>
    </ul>
    <h3 id="navigation-1">Navigation</h3>
    <ul>
      <li>
        <a href="https://docs.python.org/3/genindex.html" title="General Index"
          >index</a
        >
      </li>
      <li>
        <a
          href="https://docs.python.org/3/py-modindex.html"
          title="Python Module Index"
          >modules</a
        >
        |
      </li>
      <li>
        <a
          href="https://docs.python.org/3/using/index.html"
          title="Python Setup and Usage"
          >next</a
        >
        |
      </li>
      <li>
        <a
          href="floatingpoint.html"
          title="15. Floating Point Arithmetic: Issues and Limitations"
          >previous</a
        >
        |
      </li>
      <li><img src="../_static/py.png" /></li>
      <li><a href="https://www.python.org/">Python</a> »</li>
      <li>
        <a href="https://docs.python.org/3/index.html">3.9.5 Documentation</a> »
      </li>
      <li><a href="index.html">The Python Tutorial</a> »</li>
      <li><div class="line-block"></div></li>
    </ul>
    <p>
      ©
      <a href="https://docs.python.org/3/copyright.html">Copyright</a>
      2001-2021, Python Software Foundation.<br />
      The Python Software Foundation is a non-profit corporation.
      <a href="https://www.python.org/psf/donations/">Please donate.</a>
    </p>
    <p>
      Last updated on May 30, 2021.
      <a href="https://docs.python.org/3/bugs.html">Found a bug</a>?<br />
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
    </p>
  </body>
</html>
